import { _ as _assert_this_initialized } from "@swc/helpers/_/_assert_this_initialized";
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _inherits } from "@swc/helpers/_/_inherits";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _create_super } from "@swc/helpers/_/_create_super";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import { EVENT } from "../enums";
import kingsInstance from "../instance";
import { ethers } from "ethers";
import { BigNumber } from "ethers/lib/ethers";
var walletInstance = null;
function getInner() {
    return function() {
        var _ref = _async_to_generator(function(method, params) {
            var _ref, gasPrice, chainId, nonce, tx, signedTx, res, e, _e_error;
            return _ts_generator(this, function(_state) {
                switch(_state.label){
                    case 0:
                        if (!walletInstance) {
                            return [
                                2,
                                Error("initialWalletError")
                            ];
                        }
                        if (!(params === null || params === void 0 ? void 0 : params[0])) {
                            return [
                                2,
                                Error("request params error")
                            ];
                        }
                        switch(method){
                            case "eth_sendTransaction":
                                return [
                                    3,
                                    1
                                ];
                        }
                        return [
                            3,
                            8
                        ];
                    case 1:
                        return [
                            4,
                            Promise.all([
                                walletInstance.getGasPrice(),
                                walletInstance.getChainId(),
                                walletInstance.getTransactionCount()
                            ])
                        ];
                    case 2:
                        _ref = _sliced_to_array.apply(void 0, [
                            _state.sent(),
                            3
                        ]), gasPrice = _ref[0], chainId = _ref[1], nonce = _ref[2];
                        tx = _object_spread({
                            value: 0,
                            type: 0,
                            nonce: nonce,
                            chainId: chainId,
                            gasPrice: gasPrice.mul(3)
                        }, params[0]);
                        tx.gasLimit = BigNumber.from(tx.gas).toNumber();
                        delete tx.gas;
                        return [
                            4,
                            walletInstance.signTransaction(tx)
                        ];
                    case 3:
                        signedTx = _state.sent();
                        _state.label = 4;
                    case 4:
                        _state.trys.push([
                            4,
                            6,
                            ,
                            7
                        ]);
                        return [
                            4,
                            walletInstance.provider.sendTransaction(signedTx)
                        ];
                    case 5:
                        res = _state.sent();
                        return [
                            2,
                            res.hash
                        ];
                    case 6:
                        e = _state.sent();
                        if ((e === null || e === void 0 ? void 0 : (_e_error = e.error) === null || _e_error === void 0 ? void 0 : _e_error.code) === -32000 && e.reason === "already known") {
                            return [
                                2,
                                e.transactionHash
                            ];
                        }
                        kingsInstance.emit(EVENT.error, e);
                        throw e;
                    case 7:
                        return [
                            3,
                            8
                        ];
                    case 8:
                        return [
                            2
                        ];
                }
            });
        });
        return function(method, params) {
            return _ref.apply(this, arguments);
        };
    }();
}
export var NFKingsWalletProvider = /*#__PURE__*/ function(_ethers_providers_Web3Provider) {
    "use strict";
    _inherits(NFKingsWalletProvider, _ethers_providers_Web3Provider);
    var _super = _create_super(NFKingsWalletProvider);
    function NFKingsWalletProvider(wallet) {
        _class_call_check(this, NFKingsWalletProvider);
        var _this;
        walletInstance = wallet;
        var inner = getInner();
        _this = _super.call(this, inner);
        _define_property(_assert_this_initialized(_this), "isNFKingsWallet", true);
        _define_property(_assert_this_initialized(_this), "changeRPCProvider", null);
        _this.changeRPCProvider = function(provider) {
            provider.getNetwork().then(function(param) {
                var chainId = param.chainId;
                walletInstance = wallet.connect(provider);
                _this.emit("networkChanged", chainId);
                kingsInstance.emit(EVENT.networkChanged, chainId);
            });
        };
        return _this;
    }
    return NFKingsWalletProvider;
}(ethers.providers.Web3Provider);
var provider = null;
export default function getKingsWalletProvider(chain, privatekey) {
    var rpcProvider = new ethers.providers.JsonRpcProvider(chain.rpcUrl, {
        chainId: chain.chainId,
        name: chain.name
    });
    if (provider) {
        provider.changeRPCProvider(rpcProvider);
        return provider;
    }
    var wallet = new ethers.Wallet(privatekey, rpcProvider);
    provider = new NFKingsWalletProvider(wallet);
    return provider;
}

 //# sourceMappingURL=index.js.map